package com.jixiaotian.spring.annotation.demo.annotation.componentscan;

import org.springframework.boot.ansi.AnsiColor;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Random;

/**
 * @Author: jixiaotian.ch@qq.com
 * @Date : 2020/9/10
 */
public class MyTypeFilter implements TypeFilter {

    private int[] colorCode = {30,31,32,33,34,35,36,37,90,91,92,93,94,95,96,97};
    private void out(String str){
        System.out.println("\033[" + String.valueOf(colorCode[new Random().nextInt(colorCode.length)])+";1m"+str+"\033[0m");
    }

    /**
     *
     * @param metadataReader  获取当前正在操作类的信息
     * @param metadataReaderFactory  获取上下文中所有的信息
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {

        //获得当前类的所有的注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        //获得当前扫描到的类的信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //获取到当前类的所有的资源信息
        Resource resource = metadataReader.getResource();
        //
        String className = classMetadata.getClassName();

        out( "--------------- " + className + " ----------------");



        if(className.contains("Controller")){

            return true;
        }

        return false;
    }
}
